<template>
  <div v-loading="loading">
    <div>
      <el-input placeholder="请输入文本后回车" v-model="searchConfig.SearchText" @change="handleSearch" style="width:200px;"></el-input>
      <el-date-picker placeholder="查询本日起往后十天的数据" v-model="searchConfig.DateBgn" style="width:250px;" @change="handleSearch" align="right" type="date"></el-date-picker>
      <el-button @click="handleClear" type="button">清空</el-button>
      &nbsp;<span>（最近半年的数据。无论有没有配置，都会记录到简单日志里面，如果有配置，则会记录在详细日志里面）</span>
    </div>
    <el-table ref="singleTable"
              :data="tableData"
              highlight-current-row
              @current-change="handleCurrentChange"
              size="mini"
              style="width: 100%">
      <el-table-column type="index"
                       width="50">
      </el-table-column>
      <el-table-column property="FileName"
                       label="文件名" :show-overflow-tooltip="true"
                       width="200">
      </el-table-column>
      <el-table-column property="Abstract"
                       label="搜索预览" :show-overflow-tooltip="true">
      </el-table-column>
      <el-table-column fixed="right"
                       label="操作"
                       width="100">
        <template slot-scope="scope">
          <el-button @click="handleDown(scope.row)" type="text" size="small">下载</el-button>
        </template>
      </el-table-column>
    </el-table>
    <br />
  </div>
</template>

<script>
  export default {
    name: 'RecentLog',
    data() {
      return {
        tableData: [],
        allData: [],
        loading: false,
        dataCount: 1,
        searchConfig: {
          DateBgn: null,
          SearchText: null
        },
        currentRow: {},
        dialogFormVisible: false,
        url: this.$store.state.serviceUrl,
        urlDown: this.$store.state.downUrl
      };
    },
    computed: {
      isSimple() {
        return this.$route.params.isSimple === "1";
      }
    },
    methods: {
      handleDown(row) {
        window.open(this.urlDown + row.FileName);
      },
      handleClear() {
        var keys = Object.keys(this.searchConfig);
        for (var i = 0; i < keys.length; i++) {
          this.searchConfig[keys[i]] = null;
        }
        this.refreshData();
      },
      handleSearch() {
        this.refreshData();
      },
      setCurrent(row) {
        this.$refs.singleTable.setCurrentRow(row);
      },
      handleCurrentChange(val) {
        this.currentRow = val;
      },
      refreshData() {
        this.loading = true;
        this.$http.post(this.url, { type: "SearchTextInLog", config: Base64.encode(JSON.stringify(this.searchConfig)), isSimple: this.isSimple}, { emulateJSON: true }).then((m) => {
          this.allData = m.body;
          this.tableData = m.body;
          this.loading = false;
        }, (e) => {
          this.$message(e.bodyText);
          this.loading = false;
        });
      }
    },
    mounted() {
      this.refreshData();
    },
    watch: {
      $route(to, from) {
        this.refreshData();
      }
    }
  };
</script>
<style>
</style>
